package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.common.SaveAddress;
import com.linzp.bishe.entity.SoftwareCopyright;
import com.linzp.bishe.mapper.SoftwareCopyrightMapper;
import com.linzp.bishe.model.SoftwareCopyrightModel;
import com.linzp.bishe.service.ParticipantsService;
import com.linzp.bishe.service.SoftwareCopyrightService;
import com.linzp.bishe.utils.DealFile;
import com.linzp.bishe.utils.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class SoftwareCopyrightServiceImpl extends ServiceImpl<SoftwareCopyrightMapper, SoftwareCopyright> implements SoftwareCopyrightService {

	@Autowired
	private SaveAddress saveAddress;

	@Autowired
	private SoftwareCopyrightMapper softwareCopyrightMapper;

	@Autowired
	private ParticipantsService participantsService;
	@Override
	public String saveMultipartFile(MultipartFile file) {
		return DealFile.saveMultipartFile(file,saveAddress.getAllAddress()+saveAddress.getCache());
	}

	@Override
	public void saveCacheToFile(String name) {
		DealFile.saveCacheToFile(saveAddress.getAllAddress()+saveAddress.getCache(),
				saveAddress.getAllAddress()+saveAddress.getSoftwareCopyright(),name);
		DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getCache()+"/"+name);
	}

	/**
	 * @Description 得到单个用户的软件所有信息
	 * @Param [success, id, current, page, searchSoft, searchStatus]
	 * @return void
	 */
	@Override
	public void getByIdAllSoftware(Result success, Integer id, Integer current,
	                               Integer page, String searchSoft, String searchStatus,String searchYear) {
		if (searchSoft!=null & !searchSoft.isEmpty()){
			searchSoft = '%'+searchSoft+'%';
		}
		success.put("tableData",softwareCopyrightMapper.getByIdAllSoftware(id, TableName.SOFTWARE, searchSoft, searchStatus,searchYear,(current-1) * page,page));
		success.put("total",softwareCopyrightMapper.getByIdAllSoftwareCounters(id, TableName.SOFTWARE, searchSoft, searchStatus,searchYear));
		success.put("loadAddress",saveAddress.getAccessPath()+saveAddress.getSoftwareCopyright());
	}

	@Override
	public String updateMultipartFile(MultipartFile file,String fileName) {
		String name = DealFile.upDateMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getSoftwareCopyright(), fileName);
		if (!name.equals(fileName)){
			DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getSoftwareCopyright()+"/"+fileName);
		}
		return name;
	}

	/**
	 * @Description 删除软件消息
	 * @Param [softWare]
	 * @return void
	 */
	@Override
	public void deleteSoftware(SoftwareCopyrightModel softWare) {
		SoftwareCopyright copyright = softWare.getSoftwareCopyright();
		this.removeById(copyright.getId());

		String materials = copyright.getSupportingMaterials();
		if (materials !=null){
			if(!materials.isEmpty()){
				DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getSoftwareCopyright()+"/"+materials);
			}
		}
		participantsService.removeListById(softWare.getParticipants());
	}

	/**
	 * @Description 处理状态
	 * @Param [id, status]
	 * @return void
	 */
	@Override
	public void dealStatus(Integer id, String status) {
		UpdateWrapper<SoftwareCopyright> wrapper = new UpdateWrapper<>();
		wrapper.eq("id",id);
		wrapper.set("status",status);
		this.update(wrapper);
	}

	@Override
	public boolean cheakRepeat(String element1) {
		QueryWrapper<SoftwareCopyright> wrapper = new QueryWrapper<>();
		wrapper.eq("copyright_name",element1);
		return this.getOne(wrapper,false)!=null ? true:false;
	}
}
